package com.smfx.yxt.controller.student;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.ParentInfo;
import com.smfx.yxt.model.ParentStudentRel;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.service.IParentService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.utils.FileUtils;
import com.smfx.yxt.view.ParentRelStudentView;
import com.smfx.yxt.view.StudentInfoView;
import com.smfx.yxt.vo.StudentInfoInputVO;
import com.smfx.yxt.wechat.message.WechatEwm;
import com.smfx.yxt.wechat.model.WechatQrcodeRsp;
import com.smfx.yxt.wechat.model.WechatUserInfo;
import com.smfx.yxt.wechat.utils.MessageUtil;
import com.smfx.yxt.wechat.utils.WechatUtils;

@RequestMapping(Const.STUDENT_ROLE + MenuConst.USER_MY_URL)
@Controller
public class StudentUserController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(StudentUserController.class);
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IParentService parentService;

	@RequestMapping(value={"","/"})
	public String my(Model model){
		AppUser appUser = getAppUser();
		List<StudentInfo> list = studentService.selectByOpenid(appUser.getOpenid());
		model.addAttribute("list", list);
		for(StudentInfo student : list) {
			if(student.getId().intValue() == appUser.getParent().getStudentId().intValue()) {
				model.addAttribute("student", student);
				break;
			}
		}
		if(StringUtils.isBlank(appUser().getParent().getHeadImgUrl())) {
			ParentInfo parent = new ParentInfo();
			parent.setId(appUser().getParent().getId());
			//WechatUserInfo userInfo = WechatUtils.getUserInfo(WechatUtils.getAccessToken(), appUser().getOpenid());
			//String headImgUrl = FileUtils.writeHttpFile(userInfo.getHeadimgurl(), RelationType.HEAD_IMG_URL.getCode());
			parent.setHeadImgUrl("sys/xs.png");
			parentService.updateForParent(parent);
			appUser().getParent().setHeadImgUrl("sys/xs.png");
		}
		return "student/my";
	}
	
	@RequestMapping("studentInfo")
	public String StudentInfo(Model model) {
		AppUser appUser = getAppUser();
		StudentInfo studentInfo = studentService.selectById(appUser.getParent().getStudentId());
		StudentInfoView student = BeanUtils.copyAs(studentInfo, StudentInfoView.class);
		model.addAttribute("student", student);
		return "student/studentInfo";
	}
	
	@RequestMapping("studentList")
	@ResponseBody Object studentList() {
		AppUser appUser = getAppUser();
		return studentService.selectByOpenid(appUser.getOpenid());
	}
	
	@RequestMapping("student/infoInput")
	@ResponseBody Object studentInfoInput(StudentInfoInputVO vo) {
		try {
			AppUser appUser = getAppUser();
			StudentInfo studentInfo = studentService.selectById(appUser.getParent().getStudentId());
			StudentInfo student = BeanUtils.copyAs(vo, StudentInfo.class);
			if(studentInfo.getInfoInputTime() == null) {
				student.setInfoInputTime(new Date());
				student.setInfoInputer(appUser.getParent().getId());
			}else {
				if(DateUtil.addDay(studentInfo.getInfoInputTime(),1).getTime() < new Date().getTime()) {
					return new RspResult(Status.STUDENT_INTO_INPUT_SUCESS);
				}
			}
			if(StringUtils.isNotBlank(vo.getBirthday())) {
				student.setBirthday(DateUtil.strToDate(vo.getBirthday(), DateUtil.DEFAULT_DATE_FORMAT3));
			}
			student.setId(appUser.getParent().getStudentId());
			studentService.updateById(student);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---studentInfoInput error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("infoUpdate")
	@ResponseBody Object infoUpdate(ParentRelStudentView record) {
		try {
			if(StringUtils.isBlank(record.getRelation())) {
				return new RspResult(Status.PARAM_FAIL);
			}
			AppUser appUser = getAppUser();
			ParentRelStudentView parent = appUser.getParent();
			if(StringUtils.isNotBlank(record.getPhone())) {
				parent.setPhone(record.getPhone());
				parent.setUpdateTime(new Date());
				parentService.updateForParent(parent);
				appUser.getParent().setPhone(record.getPhone());
			}
			ParentStudentRel rel = new ParentStudentRel();
			rel.setUpdateTime(new Date());
			rel.setId(parent.getRelId());
			rel.setRelation(record.getRelation());
			parentService.updateForParentRelStudent(rel);
			appUser.getParent().setRelation(record.getRelation());
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---infoUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("getInviteEwm")
	@ResponseBody Object getInviteEwm(){
		try {
			AppUser appUser = getAppUser();
			ParentRelStudentView parent = appUser.getParent();
			if(StringUtils.isNotBlank(parent.getInviteEwm()) && 
					DateUtil.compare(new Date(), parent.getEwmInvalidTime()) == -1) {
				return new RspResult(Status.SUCCESS, parent.getInviteEwm());
			}
			WechatQrcodeRsp rsp = WechatUtils.getQrcode(MessageUtil.QR_STR_SCENE, 
					WechatEwm.INVITE_BIND_STU_EWM.getCode(), String.valueOf(parent.getStudentId()));
			if(rsp != null) {
				ParentStudentRel rel = new ParentStudentRel();
				rel.setInviteEwm(rsp.getQrcode());
				rel.setEwmInvalidTime(DateUtil.addDay(28));
				rel.setUpdateTime(new Date());
				rel.setId(parent.getRelId());
				parentService.updateForParentRelStudent(rel);
				appUser.getParent().setInviteEwm(rel.getInviteEwm());
				appUser.getParent().setEwmInvalidTime(rel.getEwmInvalidTime());
				return new RspResult(Status.SUCCESS, rsp.getQrcode());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
}
